layui.use(['form','jquery', 'dtree', 'iconPicker', 'backend'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    let dtree = layui.dtree;
    let backend = layui.backend;
    let iconPicker = layui.iconPicker;

    dtree.render({
        elem: "#form-pid",
        initLevel: "1",
        method: 'get',
        response: {
            statusName: "code",
            statusCode: "1",
            message:"msg",
            rootName:"data",
            treeId:"id",
            parentId:"pid",
            title:"title",
            basicData: "basicData",
            childName: "children"
        },
        url: "/admin/menu/select",
        select: true,
        dataStyle:"layuiStyle",
        selectCardHeight: "150",
        selectInputName: {
            nodeId: "form[pid]",
            context: ""
        }
    });

    iconPicker.render({
        // 选择器，推荐使用input
        elem: '#form-icon',
        // 数据类型：fontClass/unicode，推荐使用fontClass
        type: 'fontClass',
        // 是否开启搜索：true/false
        search: true,
        // 是否开启分页
        page: true,
        // 每页显示数量，默认12
        limit: 16
    });

    form.on('submit(save)', function(data){
        let loading = layer.load();
        $.ajax({
            url: data.form.action,
            data: data.field,
            type:'post',
            success:function(res){
                if(res.code == 1){
                    layer.msg(res.msg,{icon:1,time:1000},function(){
                        backend.api.close();
                    });
                }else{
                    layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                        layer.close(index);
                    });
                }
            },
            complete : function () {
                layer.close(loading);
            }
        });
        return false;
    });
});
